/**
 * 
 */
package net.mikoo.seals.agent.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import net.mikoo.seals.agent.biz.services.MenuService;
import net.mikoo.seals.agent.common.BaseController;
import net.mikoo.seals.agent.model.Menu;

/**
 * 
 *                       
 * @Filename MenuController.java
 *
 * @Description 菜单管理
 *
 * @Version 1.0
 *
 * @Author huangxiaoming
 *
 * @Email huangxm129@163.com
 *       
 * @History
 *<li>Author: huangxiaoming</li>
 *<li>Date: 2015年1月6日</li>
 *<li>Version: 1.0</li>
 *<li>Content: create</li>
 *
 */
@Controller
@RequestMapping("/admin/menu")
public class MenuController extends BaseController {
	
	Logger				logger	= Logger.getLogger(MenuController.class);
	
	@Autowired
	private MenuService	menuService;
	
	@RequestMapping("/index")
	public String index(Long parentId, Model model) {
		if (parentId == null) {
			parentId = 0L;
		}
		List<Menu> menus = menuService.loadAsTree();
		for (Menu menu : menus) {
			System.out.println(menu.getMenuName());
		}
		model.addAttribute("menus", menus);
		return resultView(ADMIN_VIEW, "menu/index");

	}
	
	@RequestMapping("/form")
	public String form(Long parentId, Model model) {
		//查询树状菜单树结构
		List<Menu> menus = menuService.loadAsTree();
		model.addAttribute("menus", menus);
		model.addAttribute("parentId", parentId);
		
		return resultView(ADMIN_VIEW, "menu/form");
	}

	@RequestMapping("/create")
	public String create(Menu menu, HttpServletRequest request, Model model) {

		menuService.save(menu);
		return redirect("/admin/menu/index.htm");
	}
	
	@RequestMapping("/edit")
	public String edit(Long menuId, Model model) {
		Menu menu = menuService.loadByPrimaryKey(menuId);
		model.addAttribute("menu", menu);
		
		//查询树状菜单树结构
		List<Menu> menus =  menuService.loadAsTree();
		model.addAttribute("menus", menus);
		
		return resultView(ADMIN_VIEW, "menu/edit");
	}
	
	@RequestMapping("/modify")
	public String modify(Menu menu, Model model) {
		menuService.modifyByPrimaryKey(menu);
		return redirect("/admin/menu/index.htm");
	}
	
	@RequestMapping("/delete")
	public String delete(Long menuId, Model model) {
		menuService.deleteByPrimaryKey(menuId);
		return redirect("/admin/menu/index.htm");
	}
}